package com.tnsw.coreutils.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;

public class ListViewPullUpdate extends ListView implements OnScrollListener {

	private OnScrollListener mOnScrollListener;
	protected String tag = "ListViewPullUpdate";
	
	public ListViewPullUpdate(Context context) {
		super(context);
		this.setOnScrollListener(this);
		Log.d(tag, "this1");
	}
	
	public ListViewPullUpdate(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.setOnScrollListener(this);
		Log.d(tag, "this2");
	}
	
	public ListViewPullUpdate(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.setOnScrollListener(this);
		Log.d(tag, "this3");
	}

	@Override
	public void setOnScrollListener(OnScrollListener listener) {
		if (listener == this) {
			super.setOnScrollListener(listener);	
		} else {
			this.mOnScrollListener = listener;	
		}
		
		
	}
	
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (this.mOnScrollListener != null) {
			this.mOnScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);	
		}
		
		//Log.d(tag, "here1");
		
		if (this.getAdapter() instanceof AdapterPullUpdate) {
			AdapterPullUpdate adapter = (AdapterPullUpdate) this.getAdapter();
			//Log.d(tag, "here2");
			
			if (this.getFirstVisiblePosition() == 0) {
				View v1 = (View) this.getChildAt(0);
				
				//Log.d(tag, "here3");
				
			}
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		
		if (this.mOnScrollListener != null) {
			this.mOnScrollListener.onScrollStateChanged(view, scrollState);
		}
		
		if (this.getAdapter() instanceof AdapterPullUpdate) {
			AdapterPullUpdate adapter = (AdapterPullUpdate) this.getAdapter();
			
			switch (scrollState) {
				case SCROLL_STATE_FLING:
					Log.d(tag, "onScrollStateChaged:SCROLL_STATE_FLING");
					break;
				
				case SCROLL_STATE_IDLE:
					Log.d(tag, "onScrollStateChaged:SCROLL_STATE_IDLE");

					break;
				
				case SCROLL_STATE_TOUCH_SCROLL:
					Log.d(tag, "onScrollStateChaged:TOUCH_SCROLL");
					break;
			}
		}
	}

}
